/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
/**
 * 
 */
package org.myontology.users;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

import org.myontology.controller.AttributeHolder;
import org.myontology.init.Globals;
import org.myontology.model.AbstractSubjectValidator;
import org.myontology.model.Subject;
import org.myontology.model.ValidatorException;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class CheckCaptcha extends AbstractSubjectValidator {

	/* (non-Javadoc)
	 * @see org.myontology.model.SubjectValidator#getWarning()
	 */
	public String getWarning() {
		return "user.capcha";
	}

	/* (non-Javadoc)
	 * @see org.myontology.model.SubjectValidator#validate(org.myontology.model.Subject, org.myontology.controller.AttributeHolder)
	 */
	public void validate(Subject s, AttributeHolder attributes) throws ValidatorException{
		String challenge=attributes.getRequestParameter("recaptcha_challenge_field");
        String response=attributes.getRequestParameter("recaptcha_response_field");
        
	    String remoteAddr = "127.0.0.1";
	    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
	    
	    // Probably don't want to hardcode your private key here but
	    // just to get it working is OK...
	    reCaptcha.setPrivateKey(Globals.recaptchaPrivate.getValue(attributes));
	    
	    ReCaptchaResponse reCaptchaResponse =
	        reCaptcha.checkAnswer(remoteAddr, challenge, response);
	    
	    if (!reCaptchaResponse.isValid()) {
	    	throw new ValidatorException(getWarning());
		}
	}
}
